<?php

$queueName = 'monitor-upload';
$exchangeName = 'monitor';
$routeKey = 'upload';
$connection = new \AMQPConnection(
        ['host' => '172.16.101.175',
    'port' => '5672',
    'vhost' => '/',
    'login' => 'guest',
    'password' => 'guest']);
$connection->connect() or die("Cannot connect to the broker!\n");
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName($exchangeName);
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->setFlags(AMQP_DURABLE);
$exchange->declareExchange();
$queue = new \AMQPQueue($channel);
$queue->setName($queueName);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();

$queue->bind($exchangeName, $routeKey);
while (TRUE) {
    $msg = $queue->get(AMQP_AUTOACK);
    if (!$msg) {
        continue;
    }
    $msg = json_decode($msg->getBody());
    $res = \Qiniu::instance()->uploadBase64Img($msg->image);
    $res = json_decode($res);
    $adress = 'http://opol1n8x0.bkt.clouddn.com/' . $res->key;
    $id = $msg->id;
    $result = \think\Db::name('monitor')->where(['id' => $id])->update(['current_img' => $adress]);
}
